package org.xhnews.ui;

import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

import org.xhnews.bo.NewsBo;

public class TabsScreen extends MainScreen implements FocusChangeListener,
		WindowFrame {
	private FunctionItemField _activeFIF;
	private Field _activeWindow;
	private FunctionItemField _tab1;
	private FunctionItemField _tab2;
	private FunctionItemField _tab3;
	private FunctionItemField _tab4;
	private FunctionItemField _tab5;
	private FunctionItemField _tab6;
	private FunctionItemField _tab7;
	private HorizontalFieldManager _tabs;
	VerticalFieldManager fieldManager = new VerticalFieldManager(
			Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR
					| Manager.VERTICAL_SCROLLBAR_MASK);
   
	public TabsScreen() {
		super(net.rim.device.api.ui.Manager.NO_VERTICAL_SCROLL);
		_tabs = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH
				| HorizontalFieldManager.HORIZONTAL_SCROLL);
		_tabs.setBackground(BackgroundFactory
				.createSolidBackground(Color.BLACK));
		add(_tabs);
		add(fieldManager);
		_tab1 = new FunctionItemField("最新", new LatestNewsListField(), this);
		_tab2 = new FunctionItemField("栏目", new ChannelListField(), this);
		_tab3 = new FunctionItemField("视频", new VideoListField(), this);
		_tab4 = new FunctionItemField("收藏", new FavorListField(), this);
		_tab5 = new FunctionItemField("消息", new MessageListField(), this);
		_tab6 = new FunctionItemField("天气", new WeatherField(), this);
		_tab7 = new FunctionItemField("监测", new RssChannelListField(), this);
		_tabs.add(_tab1);
		_tabs.add(_tab2);
		_tabs.add(_tab3);
		_tabs.add(_tab4);
		_tabs.add(_tab5);
		_tabs.add(_tab6);
		_tabs.add(_tab7);
		doDefault();		
	}

	private void doDefault() {
		_activeFIF=_tab1;
		_activeWindow=(Field) _activeFIF.getSw();
		fieldManager.add(_activeWindow);	
		addMenuItem(_setting);
	}

	public void focusChanged(Field field, int eventType) {
		if (eventType == FOCUS_GAINED
				&& (field != _tab7 || field != _tab7.getSw())) {
			removeMenuItem(_appendRSSChnnl);
			removeMenuItem(_editRSSChnnl);
			removeMenuItem(_deleteRSSChnnl);
		}
		if (eventType == FOCUS_GAINED
				&& (field != _tab1 || field != _tab1.getSw())) {
			removeMenuItem(_favor);
			removeMenuItem(_forward);
		}
		if (eventType == FOCUS_GAINED
				&& (field != _tab4 || field != _tab4.getSw())) {
			removeMenuItem(_clearfavor);
		}
		if (eventType == FOCUS_GAINED) {
			if (field instanceof FunctionItemField)
				updateFunction((FunctionItemField) field);
			if (field == _tab1) {
				addMenuItem(_favor);
				addMenuItem(_forward);
			} else if (field == _tab4) {
				addMenuItem(_clearfavor);
			} else if (field == _tab7) {
				addMenuItem(_appendRSSChnnl);
				addMenuItem(_editRSSChnnl);
				addMenuItem(_deleteRSSChnnl);
			}
		}
	}

	public void updateWindow(Field field) {
		if(!field.equals(_activeWindow))
		fieldManager.replace(_activeWindow, field);
		_activeWindow=field;
	}
	private void updateFunction(FunctionItemField field) {
		_activeFIF.setState(FunctionItemField.COLD_STATE);
		_activeFIF.doChange();		
		_activeFIF=field;		
		_activeFIF.setState(FunctionItemField.HOT_STATE);
		_activeFIF.doChange();
	}

	protected void makeMenu(Menu menu, int context) {
		super.makeMenu(menu, context);
	}

	public boolean onClose() {
		if (Dialog.ask(Dialog.D_YES_NO, "确定要退出系统？") == Dialog.YES) {
			System.exit(0);
		}
		return true;
	}

	public MenuItem _favor = new MenuItem("收藏", 110, 10) {
		public void run() {
			NewsBo.setItemFavor(((LatestNewsListField) _tab1.getSw())
					.getSelectedNewsId());
			((LatestNewsListField) _tab1.getSw()).reload();
		}
	};
	public MenuItem _clearfavor = new MenuItem("取消收藏", 110, 10) {
		public void run() {
			NewsBo.clearItemFavor(((FavorListField) _tab4.getSw())
					.getSelectedNewsId());
			((FavorListField) _tab4.getSw()).reload();
		}
	};
	public MenuItem _forward = new MenuItem("转发", 110, 10) {
		public void run() {
			ForwardScreen fs = new ForwardScreen();
			fs.initialize(((LatestNewsListField) _tab1.getSw())
					.getSelectedNews());
			UiApplication.getUiApplication().pushScreen(fs);
		}
	};

	public MenuItem _appendRSSChnnl = new MenuItem("添加", 110, 10) {
		public void run() {
			((RssChannelListField) _tab7.getSw()).append();
		}
	};
	public MenuItem _editRSSChnnl = new MenuItem("修改", 110, 10) {
		public void run() {
			((RssChannelListField) _tab7.getSw()).edit();
		}
	};
	public MenuItem _deleteRSSChnnl = new MenuItem("删除", 110, 10) {
		public void run() {
			((RssChannelListField) _tab7.getSw()).del();
		}
	};

	public MenuItem _setting = new MenuItem("设置", 110, 10) {
		public void run() {
			UiApplication.getUiApplication().pushScreen(new SettingsScreen());
		}
	};
	protected boolean keyChar(char key, int status, int time) {
		if (key == Characters.ESCAPE) {
			_activeFIF.setFocus();
		}
		return true;
	}
}
